﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace InfoPathTranslate
{
    interface XsltFunction
    {
        InfoPathField Execute(InfoPathField[] args);
        InfoPathFieldType GetReturnType();
    }

    class XsltFunctionManager
    {

        private static Hashtable functions = null;

        private static void Init() 
        {
            functions = new Hashtable();

            functions.Add("string", new Xslt_string());


            functions.Add("true", new Xslt_true());
            functions.Add("false", new Xslt_false());
        }

        public static InfoPathField ExecuteFunction(String name, InfoPathField[] args)
        {
            if (functions == null)
            {
                Init();
            }

            XsltFunction f = (XsltFunction)functions[name];
            if (f == null)
            {
                return null;
            }

            return f.Execute(args);
        }

        public static InfoPathFieldType GetReturnType(String name)
        {
            if (functions == null)
            {
                Init();
            }

            XsltFunction f = (XsltFunction)functions[name];
            if (f == null)
            {
                return InfoPathFieldType.String;
            }

            return f.GetReturnType();
        }

    }

// http://www.w3school.com.cn/xpath/xpath_functions.asp#string
    
    //存取

    //错误和跟踪

    //数值

    //字符串
    class Xslt_string : XsltFunction
    {
        public InfoPathFieldType GetReturnType() 
        {
            return InfoPathFieldType.String;
        }

        public InfoPathField Execute(InfoPathField[] args)
        {
            if (args.Length != 1) return null;

            InfoPathString f = new InfoPathString(args[0].GetStringValue());
            return f;
        }
    }

    //AnyURI

    //逻辑
    class Xslt_true : XsltFunction
    {
        public InfoPathFieldType GetReturnType()
        {
            return InfoPathFieldType.Boolean;
        }

        public InfoPathField Execute(InfoPathField[] args)
        {
            InfoPathBoolean f = new InfoPathBoolean(true);
            return f;
        }
    }

    class Xslt_false : XsltFunction
    {
        public InfoPathFieldType GetReturnType()
        {
            return InfoPathFieldType.Boolean;
        }

        public InfoPathField Execute(InfoPathField[] args)
        {
            InfoPathBoolean f = new InfoPathBoolean(false);
            return f;
        }
    }
    //持续时间/日期/时间

    //QName

    //节点

    //序列

    //Context
}
